package com.shaozi.nc.io.mq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.commons.lang.NotImplementedException;
import org.apache.log4j.Logger;

import com.shaozi.data.mq.protocol.ConnectionBuilder;
import com.shaozi.data.mq.protocol.SubScriberNameCallback;
import com.shaozi.data.mq.protocol.stomp.StompTextConsumer;
import com.shaozi.lib.throwable.SzException;

/**
 * 账号状态变化
 * 
 * @author brenx
 * 
 */
public class AccountStateConsumer extends StompTextConsumer {

	private static final Logger logger = Logger.getLogger(AccountStateConsumer.class);
	private String compId;

	public AccountStateConsumer(ConnectionBuilder connBuilder) {
		super(connBuilder);
	}

	/**
	 * 启动监听帐号状态变化
	 * 
	 * @param consumerName
	 *            每台机器对应一个消费的ID
	 */
	public void listenAccountState(String consumerName) {
		final String cName = consumerName;
		setSubScriberNameCallback(new SubScriberNameCallback() {

			@Override
			public String onSubScriberName() {
				return cName + ".account.state";
			}
		});
		addMessageListener(new ConsumerMessageListener());
	}

	@Override
	protected String getDestName() {
		return "im_" + compId;
	}

	private class ConsumerMessageListener implements MessageListener {

		@Override
		public void onMessage(Message msg) {
			TextMessage mm = (TextMessage) msg;
			try {
				System.out.println(mm.getText());

			} catch (JMSException e) {
				logger.error(SzException.getStackMessage(e));
			}
		}

	}

	@Override
	protected String getDurableSubScriberName() {
		throw new NotImplementedException("not impl");
	}

}
